package com.example.threadTest01;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * description: TaskTest <br>
 * date: 2021/2/22 09:45 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class TaskTest {

    public static void main(String[] args) {

        MyTask myTask = new MyTask();
        FutureTask<Object> futureTask = new FutureTask<>(myTask);

        // FutureTask继承了Runnable接口，可以放在Thread中启动执行
        Thread thread = new Thread(futureTask);
        thread.setName("taskDemo");
        thread.start();
        System.out.println("主线程名称："+Thread.currentThread().getName());

        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            // 阻塞等待中被中断，则抛出
            e.printStackTrace();
        } catch (ExecutionException e) {
            // 执行过程发送异常被抛出
            e.printStackTrace();
        }

    }
}
